import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";

@Injectable({
    providedIn: 'root'
})
export class ConfigService {

    public config : any;

    public app: any;
    public menus: any;

    //use
    public api: any; 

    constructor(private http: HttpClient) {
    }

    load(url) : any {
        return new Promise((resolve) => {
            this.http.get((url || "assets/json/config.json") + '?v=' + (new Date()).getTime())
                .subscribe((data :any) => {
                    this.config = data;
                    this.app = data.app;
                    this.api = data.api;
                    this.api.download_api = this.api.url + "/files";
                    this.api.upload_api = this.api.url + "/upload";
                    this.menus = data.menus;
                    resolve(this.config);
                });
        });
    }

    guid() {
        const timestamp = (new Date().getTime() / 1000 | 0).toString(16);
        return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
            return (Math.random() * 16 | 0).toString(16);
        }).toLowerCase();
    }

}
